#include<list>
#include<algorithm>
#include<iostream>
#include<fstream>
#include<regex>
#include<sstream>
#include<Windows.h>
using namespace std;

void  GetFiles(string path, list<string>& filesName)
{
	WIN32_FIND_DATAA ffd;
	memset(&ffd, 0, sizeof(ffd));
	path.append("\\*");
	HANDLE hFind = FindFirstFileA(path.c_str(), &ffd);
	do {
		if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
		}
		else
		{
			char filePath[256] = { 0 };
			sprintf_s(filePath, "%s/%s", path.c_str(), ffd.cFileName);
			filesName.push_back(ffd.cFileName);
			//filesPath.push_back(filePath);
		}
	} while (FindNextFileA(hFind, &ffd) != 0);
	FindClose(hFind);
}

void DoParse(string strTemp, list<string>& listFuncAndParam)
{
	regex pattern1(R"REG((\w+)[ \t]+(\w+)\()REG");
	smatch result;
	string::const_iterator iterStart = strTemp.begin();
	string::const_iterator iterEnd = strTemp.end();

	regex_search(iterStart, iterEnd, result, pattern1);
	cout << result[2] << endl;
	//listFuncAndParam.push_back(result[2]);

	iterStart = result[0].second;

	regex pattern2(R"REG( ?(\w+)[,)])REG");
	while (regex_search(iterStart, iterEnd, result, pattern2))
	{
		cout << result[1] << endl;
		if (result[1] != "void")
		{
			listFuncAndParam.push_back(result[1]);
		}
		
		iterStart = result[0].second;
	}
}

void DoGenerateComment(list<string> listFuncnameAndParam, string& strComment)
{
	strComment += "/**\n";
	strComment += " * @brief \n";
	for (auto c : listFuncnameAndParam)
	{
		strComment += " * @param[in] ";
		strComment += c;
		strComment += " \n";
	}
	strComment += " * @return \n";
	strComment += " */\n";
}

int main(int argc, char* argv[])
{	
	list<string> listFiles;
	GetFiles(".", listFiles);

	for (auto c : listFiles)
	{
		if (c.back() != 'c')
		{
			continue;
		}
		string strInFile;
		string strFileName(c.c_str());

		//string strFileName("nPhyActiveHandle.c");

		string strFileOutput = strFileName + ".mod";

		ifstream ifs(strFileName.c_str());
		ostringstream tmp;
		tmp << ifs.rdbuf();
		strInFile = tmp.str();

		string strHeaderComment;
		strHeaderComment += "/**\n";
		strHeaderComment += " * Copyright(c) 2019, Big Fish Semi Ltd.\\n\n";
		strHeaderComment += " * All rights reserved.\\n\n";
		strHeaderComment += " *\n";
		strHeaderComment += " * @file ";
		strHeaderComment += strFileName.substr(strFileName.find_last_of('\\') + 1);
		strHeaderComment += " \n";
		strHeaderComment += " * @brief \n"
			" * @author \n"
			" *\n"
			" */\n";
		strInFile.insert(strInFile.begin(), strHeaderComment.begin(), strHeaderComment.end());


		regex pattern(R"REG(\w+[ \t]+\w+\(.*\)\n\{)REG");

		string::const_iterator iterStart = strInFile.begin();
		string::const_iterator iterEnd = strInFile.end();
		string temp;
		smatch result;
		while (regex_search(iterStart, iterEnd, result, pattern))
		{
			temp = result[0];

			list<string> listFuncnameAndParam;
			cout << temp << " ";
			cout << endl;
			DoParse(temp, listFuncnameAndParam);

			string strComment;
			DoGenerateComment(listFuncnameAndParam, strComment);

			cout << "\n=========================================================\n";

			iterStart = result[0].second;
			int curPos = iterStart - strInFile.begin();

			strInFile.insert(result[0].first, strComment.begin(), strComment.end());

			curPos += strComment.size();
			iterStart = strInFile.begin() + curPos;
			iterEnd = strInFile.end();
		}

		ofstream ofs(strFileOutput.c_str());
		ofs << strInFile;
	}

	

	return 0;
}
